package com.ruoyi.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.result.Result;
import com.ruoyi.domain.T06;
import com.ruoyi.domain.T18;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.service.IT07Service;
import com.ruoyi.service.IT18Service;
import com.ruoyi.service.impl.T07ServiceImpl;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.impl.SysRoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.util.List;
import com.ruoyi.common.core.controller.BaseController;



@RestController
public class KHDController extends  BaseController{
    @Autowired
    private IT18Service t18Service;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private T07ServiceImpl t07ServiceImpl;
    @Autowired
    private SysRoleServiceImpl sysRoleService;

    @Autowired
    private IT07Service t07Service;

    @GetMapping("/T18/list")
    @Log(title = "管理员")
    public TableDataInfo list(HttpServletRequest request, T18 t18) {
        startPage();
        LoginUser loginUser =tokenService.getLoginUser(request);
        Long userId=loginUser.getUserId();
        Long ComId=t18Service.selectF0301ByF1801(userId);
        List<T18> list = t18Service.selectT18ByComIdList(t18,ComId);
        return getDataTable(list);
    }
    @PostMapping("/T18/addUser")
    public AjaxResult adminregister(@RequestBody T18 t18, HttpServletRequest request) {
        try {
            LoginUser loginUser =tokenService.getLoginUser(request);
            Long userId=loginUser.getUserId();
            Long ComId=t18Service.selectF0301ByF1801(userId);
            RegisterBody registerBody=new RegisterBody();
            registerBody.setName(t18.getF1804());
            registerBody.setPassword(t18.getF1802());
            registerBody.setUsername(t18.getF1806());
            String msg = t18Service.addUser(registerBody,ComId,t18);
            return AjaxResult.success(msg);
        } catch (Exception e) {
            return AjaxResult.error();
        }
    }

    //统计
    @GetMapping("/getYYdData")
    @Log(title = "首页")
    public Result getComweekData(HttpServletRequest request) {
        try {
            return t07Service.getComweekData();
        } catch (Exception e) {
            return Result.erro("获取失败："+e.getMessage());
        }
    }
    @GetMapping("/getKHDData")
    @Log(title = "首页")
    public Result getKHDData(HttpServletRequest request) {
        try {
            return t07Service.getTotalCompany(request);
        } catch (Exception e) {
            return Result.erro("获取失败："+e.getMessage());
        }
    }
    @GetMapping("/getPre")
    @Log(title = "首页")
    public Result getPre(HttpServletRequest request) {
        try {
            SysUser user = tokenService.getLoginUser(request).getUser();
            List<Long> longs = sysRoleService.selectRoleListByUserId(user.getUserId());
            Long roleId = longs.get(0);
            return Result.success(roleId,"完成");
        } catch (Exception e) {
            return Result.erro("获取失败："+e.getMessage());
        }
    }

}

